#include<func.h>
//使用多路复用实现想说就说
int main(int argc,char *argv[])
{
    ARGS_CHECK(argc,3)
    int fdr,fdw;
    fdw=open(argv[1],O_WRONLY);
    fdr=open(argv[2],O_RDONLY);
    fd_set fdset;
    int ret;
    int read_num;
    struct timeval t;
    while(1)
    {   
        char buf[128]={0};
        bzero(&t,sizeof(t));
        t.tv_sec=3;
        FD_ZERO(&fdset);//清空fdset
        FD_SET(STDIN_FILENO,&fdset);//添加检查端口
        FD_SET(fdr,&fdset);
        ret=select(fdr+1,&fdset,NULL,NULL,&t);//fdset是传入传出参数
        if(ret>0)//等待时间到时select会返回0，其余时候会返回其他值，检测是否为0判断等待时间是否结束
        {
            if(FD_ISSET(STDIN_FILENO,&fdset))
            {
                //写程序  
                read_num=read(STDIN_FILENO,buf,sizeof(buf));
                if(0==read_num)//检测本方是否离开
                {
                    printf("I want to leave\n");
                    break;
                }
                printf("%s\n",buf);
                write(fdw,buf,strlen(buf)-1);
            }
            if(FD_ISSET(fdr,&fdset))
            {
                //读程序
                memset(buf,0,sizeof(buf));
                read_num=read(fdr,buf,sizeof(buf));
                if(0==read_num)//当对端离开，自动退出程序
                {
                    printf("byebye\n");
                    break;
                }
                printf("%s\n",buf);
            }
        }else{
            printf("he is doing someting\n");
        }
    }
    close(fdr);
    close(fdw);
    return 0;
}